package MapEditor.Editor
{
	import Engine.UIControl.UIMouse;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	
	public class Map extends Sprite
	{
		private static var DOUBLE_CLICK_TIME:int = 200;
		private var _bounds:Rectangle = null;   //拖动GUI约束矩阵
		private var _xySprite:Sprite = null;
		private var _xyTextFd:TextField = null;
		private var _doubleClickFun:Function = null;
		
		private var _clickIntval:int = 0;
		private var _ClickTimerIntval:int = 0;
		
		public function Map(width:int)
		{
			super();
			this.graphics.lineStyle();
			this.graphics.beginFill(0x111111,0.6);
			this.graphics.drawRect(0, 0, width,410);
			this.graphics.endFill();
			
			
			this._xySprite = new Sprite();
			this._xySprite.graphics.lineStyle();
			this._xySprite.graphics.beginFill(0x000000,1.0);
			this._xySprite.graphics.drawRect(0, 0, 60,40);
			this._xySprite.graphics.endFill();
			this.addChild(this._xySprite);
			
			this._xyTextFd = new TextField();
			this._xyTextFd.multiline = false;
			this._xyTextFd.textColor = 0xffffff;
			this._xySprite.addChild(this._xyTextFd);
			
			this.y = 70;
			
			this._bounds = new Rectangle(800-width,this.y,width-600,0)
			
			this.addEventListener(MouseEvent.MOUSE_DOWN,_onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP,_onMouseUp);
			this.addEventListener(MouseEvent.MOUSE_MOVE,_onMouseMove);
			
			this.buttonMode = true;
			
		}
		
		private function _onMouseDown(e:MouseEvent):void
		{
			this._clickIntval++;
			if(!this._ClickTimerIntval)
				this._ClickTimerIntval = setTimeout(onClickBack,DOUBLE_CLICK_TIME,e);
		}
		
		private function onClickBack(e:MouseEvent):void
		{
			if(this._clickIntval > 1)
				this._onDoubleClick(e);
			else
				this.onDragMap(e);
			this._clickIntval = 0;
			this._ClickTimerIntval = 0;
		}
		
		private function onDragMap(e:MouseEvent):void
		{
			UIMouse.hide();
			this.startDrag(false,this._bounds);
		}
		
		private function _onMouseUp(e:MouseEvent):void
		{
			this.stopDrag();
			UIMouse.show();
		}
		
		
		private function _onDoubleClick(e:MouseEvent):void
		{
			this._doubleClickFun(e)
		}
		
		public function setDouClickFun(fun:Function):void
		{
			this._doubleClickFun = fun;
		}
		
		
		private function _onMouseMove(e:MouseEvent):void
		{
			this._xySprite.x = this.mouseX+20;
			this._xySprite.y = this.mouseY;
			this._xyTextFd.text = String(this.mouseX)+","+String(this.mouseY);
		}
		
	}
}